import fetch from 'common/src/utils/fetch';

/**
 * 出版社列表
 */
export function findPublishingHouseList(data) {
  return fetch({
    url: '/api/textbookservice/publishingHouse/list',
    method: 'post',
    data
  });
}

/**
 * 查看单个出版社信息
 */
export function getPublishingHouseById(data) {
  return fetch({
    url: '/api/textbookservice/publishingHouse?id=' + data,
    method: 'get'
  });
}

/**
 * 新增出版社
 */
export function addPublishingHouse(data) {
  return fetch({
    url: '/api/textbookservice/publishingHouse',
    method: 'post',
    data
  });
}

/**
 * 修改出版社
 */
export function updatePublishingHouse(data) {
  return fetch({
    url: '/api/textbookservice/publishingHouse',
    method: 'put',
    data
  });
}

/**
 * 删除出版社
 */
export function delPublishingHouse(data) {
  return fetch({
    url: '/api/textbookservice/publishingHouse',
    method: 'delete',
    data
  });
}

/**
 * 导出出版社信息
 */
export function exportPublishingHouse(data) {
  let url =
    '/api/textbookservice/publishingHouse/exportExcelPublishingHouseList';
  downloadFile(data, url, 'exportPublishingHouse.xlsx');
}

function downloadFile(data, url, fileName) {
  fetch({
    url: url,
    method: 'POST',
    data,
    responseType: 'blob'
  }).then(resp => {
    if (!resp) {
      return;
    }
    let url = window.URL.createObjectURL(new Blob([resp]));
    let link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    link.setAttribute('download', fileName);
    document.body.appendChild(link);
    link.click();
  });
}


